package com.muzi.weshop.view.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.jakewharton.rxbinding3.widget.RxTextView;
import com.jakewharton.rxbinding3.widget.TextViewTextChangeEvent;
import com.muzi.weshop.R;
import com.muzi.weshop.common.LoginManager;
import com.muzi.weshop.common.base.BaseActivity;
import com.muzi.weshop.common.contants.RequestCodeContants;
import com.muzi.weshop.model.PersonnelModel;
import com.muzi.weshop.view.adapter.ContactAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NewAddressActivity extends BaseActivity {


    private static final int INTERVAL = 500;
    private PopupWindow contactPopupWindow;

    private List<PersonnelModel> contactNameList;

    @BindView(R.id.edtReceiver)
    EditText edtReceiver;
    @BindView(R.id.edtProvince)
    EditText edtProvince;
    @BindView(R.id.edtArea)
    EditText edtArea;
    @BindView(R.id.edtCity)
    EditText edtCity;
    @BindView(R.id.edtDetailAdr)
    EditText edtDetailAdr;

    private boolean canSearch;


    private int currentPid = -1;


    private ContactAdapter contactAdapter;

    @Override
    protected boolean hideStatusBar() {
        return true;
    }

    @Override
    protected int attachedLayout() {
        return R.layout.activity_new_address;
    }


    @Override
    protected void initViews() {
        createPopupWindow();


        contactNameList = new ArrayList<>();
        addTextListener();
    }


    /**
     * 创建一个PopupWindow窗体
     */
    private void createPopupWindow() {
        canSearch = false;
        View view = View.inflate(this, R.layout.popup_contact, null);
        RecyclerView rvContact = view.findViewById(R.id.rvContact);
        rvContact.setLayoutManager(new LinearLayoutManager(this));
        rvContact.setNestedScrollingEnabled(false);
        contactAdapter = new ContactAdapter(R.layout.item_contact, contactNameList);
        rvContact.setAdapter(contactAdapter);
        contactAdapter.setOnItemClickListener(new ContactAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(PersonnelModel data) {
                currentPid = data.getId();
                edtReceiver.setText(data.getAccount());
                contactPopupWindow.dismiss();
            }

        });
        contactPopupWindow = new PopupWindow(view, SizeUtils.dp2px(300), ViewGroup.LayoutParams.WRAP_CONTENT);
        contactPopupWindow.setOutsideTouchable(true);
        contactPopupWindow.setTouchable(true);
        contactPopupWindow.setBackgroundDrawable(new ColorDrawable());
        contactPopupWindow.setFocusable(false);
    }


    /**
     * 收货人  文本框监听
     */
    private void addTextListener() {
        RxTextView.textChangeEvents(edtReceiver)
                .debounce(INTERVAL, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<TextViewTextChangeEvent>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(TextViewTextChangeEvent textViewTextChangeEvent) {
                        if (!canSearch) {
                            return;
                        }
                        contactNameList.clear();
                        if (LoginManager.getInstance().getPersonal().getAccount().contains(textViewTextChangeEvent.getText().toString())) {
                            contactNameList.add(LoginManager.getInstance().getPersonal());
                        }
                        contactAdapter.setNewData(contactNameList);
                        contactPopupWindow.showAsDropDown(edtReceiver);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                canSearch = true;
            }
        }, 1000);
    }

    @Override
    protected void requestData() {

    }


    @OnClick({R.id.imgBtnBack, R.id.tvSaveAdr})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgBtnBack:
                finish();
                break;
            case R.id.tvSaveAdr:
                apiPresenter.SaveAddress(edtDetailAdr.getText().toString(), edtArea.getText().toString(), edtCity.getText().toString(), currentPid, edtProvince.getText().toString(), RequestCodeContants.SAVE_ADDRESS);
                break;
            default:
                break;
        }
    }


    @Override
    public void onNext(Object o, int requestCode) {
        super.onNext(o, requestCode);
        switch (requestCode) {
            case RequestCodeContants.SAVE_ADDRESS:
                ToastUtils.showShort("保存成功");
                finish();
                break;
            default:
                break;
        }
    }


    @Override
    public void onError(Throwable e, int request) {
        super.onError(e, request);
        switch (request) {
            case RequestCodeContants.SAVE_ADDRESS:
                ToastUtils.showShort("保存失败");
                break;
            default:
                break;
        }
    }
}
